package cn.bob.basic.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * @author bob
 * @data 2022/10/17-9:31
 */

public class EmailUtil {
    // 注入邮件对象
//    @Autowired
//    private static JavaMailSender javaMailSender;

    public static JavaMailSenderImpl JavaMailSender() {
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setHost("smtp.qq.com");
        mailSender.setUsername("209545769@qq.com");
        mailSender.setPassword("cvdxsqupydaqbjgd");
        return mailSender;
    }

    public  static  void sendMail(String serder,String recipient,String address,String enclosure) throws Exception{
        JavaMailSenderImpl javaMailSender = EmailUtil.JavaMailSender();
        // 1.创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        // 2.发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom(serder);
        helper.setSubject("店铺审核的邮件");
        // 3.设置文本内容，第二个参数 true 代表是否是一个html内容
        helper.setText("<h1>审核结果</h1>"+
                address,true);
        // 4.添加附件
        helper.addAttachment("文件.jpg",new File(enclosure));
        // 5.设置收件人
        helper.setTo(recipient);
        // 6.发送邮件
        javaMailSender.send(mimeMessage);
    }
}
